using System;
using System.Collections.Generic;
using System.Net.WebSockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace lhservice.common.websocket
{
  public class PushMsg { 
    public string action { get; set; }
    public string content { get; set; }
  }
  
  public class WebsocketClient
  {
    public WebSocket WebSocket { get; set; }
    public string CreateTime { get; set; }
    public string StationId { get; set; }    
    public string Station { get; set; }
    public string Id { get; set; }
    public string Platform { get; set; }
    public List<string> SessionList{ get; set; }
    public string UserId { get; set; }
    public string UserName { get; set; }

    public Task SendMessageAsync(string msgtype,string content)
    {
      var msgObj = new PushMsg
      {
        action = msgtype,
        content = content
      };
      var pushmsg = JsonConvert.SerializeObject(msgObj);
      var msg = Encoding.UTF8.GetBytes(pushmsg);
      return WebSocket.SendAsync(
        new ArraySegment<byte>(msg, 0, msg.Length), 
        WebSocketMessageType.Text, 
        true, 
        CancellationToken.None
      );
    }
  }
}
